﻿/*******************************************************************
 * 作者：JOHN
 * 类型：首页图表汇总
 * 时间：2011-11-25
 * 说明：
 * ****************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;

namespace HZ.Supervise.Web.Secondphase.NewReport
{
    public partial class TabulateData : BasePage
    {
        HZ.Supervise.IBLL.IExecuteContext i = HZ.Supervise.BLLFactory.ExecuteContext();

        DataTable dt = new DataTable();
        DataTable dtByMonth = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (CurrentAccount.GetUser().Org.Area.AreaID == Constants.CURRENT_DEPLOY_AREAID)
                {
                    DataBind();
                    BindMsChart();
                }
                else
                {
                    Response.Redirect("EmputyPage.aspx");
                }
            }
        }

        /// <summary>
        /// todo：绑定方法
        /// JOHN
        /// </summary>
        private void DataBind()
        {
            string where = "";

            ///todo:绑定Mschart图表
            dt = i.GetAmountType(where);

            int t =0;
            dtByMonth = i.GetAmountByMonth(where, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out t);

            AspNetPager1.RecordCount = t;
            this.gvwProcureCatalog.DataSource = dtByMonth;
            this.gvwProcureCatalog.DataBind();
        }

        /// <summary>
        /// todo:图表绑定
        /// by 张雄
        /// </summary>
        private void BindMsChart()
        {
            //空为初始页
            if (dt.Rows.Count > 0)
            {
                string AmountType = "";//金额类别     

                #region mschart金额汇总图表  柱状

                //***************** 采购总金额 *****************/
                Series series1 = new Series("采购总金额");
                series1.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Column", true);
                series1.IsValueShownAsLabel = true;
                //series1.MarkerStyle = MarkerStyle.Diamond;
                series1.BorderWidth = 1;
                series1.ShadowOffset = 1;
                series1.ToolTip = "采购总金额: #VAL{c} 万元";
                series1.Label = "#VAL{F}";

                series1.Color = Color.FromArgb(243, 84, 37);


                //***************** 配送总金额 *****************/
                Series series2 = new Series("配送总金额");
                series2.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Column", true);
                series2.IsValueShownAsLabel = true;
                //series2.MarkerStyle = MarkerStyle.Diamond;
                series2.BorderWidth = 1;
                series2.ShadowOffset = 1;
                series2.ToolTip = "配送总金额: #VAL{c} 万元";

                series2.Label = "#VAL{F}";
                series2.Color = Color.FromArgb(243, 152, 0);


                //***************** 入库总金额 *****************/
                Series series3 = new Series("入库总金额");
                series3.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Column", true);
                series3.IsValueShownAsLabel = true;
                //series3.MarkerStyle = MarkerStyle.Diamond;
                series3.BorderWidth = 1;
                series3.ShadowOffset = 1;
                series3.ToolTip = "入库总金额: #VAL{c} 万元";

                series3.Label = "#VAL{F}";
                series3.Color = Color.FromArgb(143, 195, 31);

                #region 河北耗材交易没有销售金额

                //***************** 销售总金额 *****************/
                //Series series4 = new Series("销售总金额");
                //series4.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Column", true);
                //series4.IsValueShownAsLabel = true;
                ////series4.MarkerStyle = MarkerStyle.Diamond;
                //series4.BorderWidth = 1;
                //series4.ShadowOffset = 1;
                //series4.ToolTip = "销售总金额: #VAL{c} 万元";

                //series4.Label = "#VAL{F}";
                //series4.Color = Color.FromArgb(137, 201, 151);

                #endregion

                //***************** 退货总金额 *****************/
                Series series4 = new Series("退货总金额");
                series4.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Column", true);
                series4.IsValueShownAsLabel = true;
                //series4.MarkerStyle = MarkerStyle.Diamond;
                series4.BorderWidth = 1;
                series4.ShadowOffset = 1;
                series4.ToolTip = "退货总金额: #VAL{c} 万元";

                series4.Label = "#VAL{F}";
                series4.Color = Color.FromArgb(88, 219, 213);


                #endregion

                #region 柱状图数据绑定

                int s = 0;//用于计数

                string str = "";//用来存储时间中月份

                //考虑去掉一个月没有数据，去掉一个月中的一条没有数据

                foreach (DataRow row in dt.Rows)
                {
                    AmountType = HZ.Supervise.Web.Function.FormatAmountTypeByName(row["ID"].ToString());//得到金额的类别

                    if (!str.Contains(AmountType))//如果字符串中没有金额的类别，也就是说该金额的类别未被查询过
                    {
                        str += AmountType + ",";//把金额的类别加入字符串

                        //以万元为单位
                        series1.Points.AddXY(AmountType, Convert.ToDouble(row["Amount"].ToString()) / 10000);
                        //以元为单位
                        //series1.Points.AddXY(AmountType, Convert.ToDouble(row["Amount"].ToString()));

                        s++;
                    }
                }

                ///todo:样式
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["ID"].ToString().Equals("1"))
                    {
                        series1.Points[i].Color = Color.FromArgb(243, 84, 37);
                        series1.Points[i].ToolTip = "采购总金额: #VAL{C} 万元";
                    }

                    if (dt.Rows[i]["ID"].ToString().Equals("2"))
                    {
                        series1.Points[i].Color = Color.FromArgb(243, 152, 0);
                        series1.Points[i].ToolTip = "配送总金额: #VAL{C} 万元";
                    }

                    if (dt.Rows[i]["ID"].ToString().Equals("3"))
                    {
                        series1.Points[i].Color = Color.FromArgb(143, 195, 31);
                        series1.Points[i].ToolTip = "入库总金额: #VAL{C} 万元";
                    }

                    if (dt.Rows[i]["ID"].ToString().Equals("4"))
                    {
                        series1.Points[i].Color = Color.FromArgb(137, 201, 151);
                        series1.Points[i].ToolTip = "退货总金额: #VAL{C} 万元";
                    }

                    //if (dt.Rows[i]["ID"].ToString().Equals("5"))
                    //{
                    //    series1.Points[i].Color = Color.FromArgb(88, 219, 213);
                    //    series1.Points[i].ToolTip = "退货总金额: #VAL{C} 万元";
                    //}
                }

                #endregion

                #region 数据绑定到图

                ChartPurchasing.Series.Add(series1);

                ChartPurchasing.Series.Add(series2);

                ChartPurchasing.Series.Add(series3);

                //ChartPurchasing.Series.Add(series4);

                ChartPurchasing.Series.Add(series4);

                #endregion

                #region 图片属性

                ChartPurchasing.ChartAreas["ChartPurchasing"].AxisX.IsMarginVisible = true;
                ChartPurchasing.ChartAreas["ChartPurchasing"].Area3DStyle.Enable3D = true;


                // Set labels font
                ChartPurchasing.Series["采购总金额"].Font = new Font("Trebuchet MS", 12);



                //int a = ChartPurchasing.Series["采购总金额"].Points.Count;
                //ChartPurchasing.Series["采购总金额"].CustomProperties = "PointWidth=20";
                //ChartPurchasing.Width = Unit.Pixel(25 * a);

                //设置柱状图
                ChartPurchasing.Series["采购总金额"]["DrawingStyle"] = "Cylinder";

                ChartPurchasing.ChartAreas["ChartPurchasing"].AxisX.MajorGrid.Interval = 0;
                ChartPurchasing.ChartAreas["ChartPurchasing"].AxisX.LineWidth = 1;
                ChartPurchasing.ChartAreas["ChartPurchasing"].AxisY.LineWidth = 1;

                ///todo:chart头变样式 
                ChartPurchasing.ChartAreas["ChartPurchasing"].AxisY.Title = "金额（万元）";
                //ChartPurchasing.ChartAreas["ChartPurchasing"].AxisX.Title = "汇总金额类型";

                #endregion
            }
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            DataBind();
        }
    }
}
